﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace Article_management_system
{
    public partial class Addmodification : Form
    {
        public Addmodification()//添加
        {
            InitializeComponent();
            this.Text = "添加";
            button1.Text = "添加";
            label1.Text = "添加数据";
        }
        public int ID;//存放ID
        public string TITLE;//存放文章标题
        public string AUTHOR;//存放文章作者

        public Addmodification(int Id,string Title,string Author)//修改
        {
            InitializeComponent();
            this.Text = "修改";
            button1.Text = "修改";
            label1.Text = "修改数据";
            textBox1.Text = Title;
            textBox2.Text = Author;
            ID = Id;
            TITLE = Title;
            AUTHOR = Author;
        }
        bool exit_Addmodification = true;//退出提示框处理使用
        private void button1_Click(object sender, EventArgs e)//添加、修改
        {
            if (button1.Text.Equals("添加"))
            {
                if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
                {
                    label8.Text = "文章标题不能为空";
                    label9.Text = "文章作者不能为空";
                    textBox1.Focus();
                }
                else if (string.IsNullOrEmpty(textBox1.Text))
                {
                    label8.Text = "文章标题不能为空";
                    textBox1.Focus();
                }
                else if (string.IsNullOrEmpty(textBox2.Text))
                {
                    label9.Text = "文章作者不能为空";
                    textBox2.Focus();
                }
                else
                {
                    string sql = $"INSERT INTO Articles (Title,Author) VALUES ('{textBox1.Text}','{textBox2.Text}')";
                    if (Sqldt.Modifydeleteadd(sql)>0)
                    {
                        MessageBox.Show("添加成功", "提示");
                        exit_Addmodification = false;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");
                    }

                }
            }
            else if (button1.Text.Equals("修改"))
            {
                if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
                {
                    label8.Text = "文章标题不能为空";
                    label9.Text = "文章作者不能为空";
                    textBox1.Focus();
                }
                else if (string.IsNullOrEmpty(textBox1.Text))
                {
                    label8.Text = "文章标题不能为空";
                    textBox1.Focus();
                }
                else if (string.IsNullOrEmpty(textBox2.Text))
                {
                    label9.Text = "文章作者不能为空";
                    textBox2.Focus();
                }
                
                else
                {

                    if (textBox1.Text.Equals(TITLE) && textBox2.Text.Equals(AUTHOR))
                    {
                        MessageBox.Show("未进行任何更改", "提示");
                    }
                    else
                    {
                        string sql = $"UPDATE Articles SET Title='{textBox1.Text}',Author='{textBox2.Text}',CreatedTime='{DateTime.Now}' WHERE Id={ID}";
                        if (Sqldt.Modifydeleteadd(sql) > 0)
                        {
                            MessageBox.Show("修改成功", "提示");
                            exit_Addmodification = false;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("修改失败", "提示");
                        }
                    }

                }
            }
            timer1.Enabled = true;
        }
        private void Addmodification_FormClosing(object sender, FormClosingEventArgs e)//关闭窗体
        {
            if (exit_Addmodification)
            {
                var hint = MessageBox.Show($"是否取消本次{button1.Text}！", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (!hint.Equals(DialogResult.OK))
                {
                    e.Cancel = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)//取消按钮
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)//用于提示自动消失
        {
            label8.Text = null;
            label9.Text = null;
            timer1.Enabled = false;
        }
    }
}
